package com.platform.controller;

import com.platform.entity.MerchantEntity;
import com.platform.entity.MerchantInfoEntity;
import com.platform.entity.MerchantInfomationEntity;
import com.platform.service.MerchantInfoService;
import com.platform.service.MerchantInfomationService;
import com.platform.service.MerchantService;
import com.platform.utils.PageUtils;
import com.platform.utils.Query;
import com.platform.utils.R;
import com.platform.utils.ShiroUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 商户系统管理员Controller
 *
 * @author liweihai
 * @email liweihai12345@sina.com
 * @date 2019-04-30 11:22:07
 */
@RestController
@RequestMapping("merchantinfo")
public class MerchantInfoController {
    @Autowired
    private MerchantInfoService merchantInfoService;
    @Autowired
    private MerchantService merchantService;
    @Autowired
    private MerchantInfomationService merchantInfomationService;

    /**
     * 查看列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("merchantinfo:list")
    public R list(@RequestParam Map<String, Object> params) {
        //查询列表数据
        Query query = new Query(params);

        List<MerchantInfoEntity> sysMerchantList = merchantInfoService.queryList(query);
        int total = merchantInfoService.queryTotal(query);

        PageUtils pageUtil = new PageUtils(sysMerchantList, total, query.getLimit(), query.getPage());

        return R.ok().put("page", pageUtil);
    }

    @RequestMapping("/queryObject/{userId}")
    public R queryObject(@PathVariable("userId")Long userId){
        MerchantInfoEntity sysMerchant = merchantInfoService.queryObject(userId);
        MerchantEntity merchantEntity = merchantService.queryObject(sysMerchant.getMerchantId());
        MerchantInfomationEntity merchantInfomationEntity = merchantInfomationService.queryObjectByMid(sysMerchant.getMerchantId());
        Map<String,Object> param = new HashMap<String, Object>();
        param.put("username",sysMerchant.getUsername());
        //param.put("password",sysMerchant.getPassword());
        param.put("userid",merchantEntity.getUserId());
        param.put("email",sysMerchant.getEmail());
        param.put("mobile",sysMerchant.getMobile());
        param.put("status",sysMerchant.getStatus());
        param.put("realname",merchantEntity.getRealname());
        param.put("name",merchantEntity.getName());
        param.put("phone",merchantEntity.getPhone());
        param.put("logo",merchantInfomationEntity.getLogo());
        param.put("address",merchantEntity.getAddress());
        param.put("idcard",merchantEntity.getIdcard());
        param.put("value",merchantEntity.getValue());
        param.put("value1",merchantEntity.getValue1());
        param.put("licence",merchantEntity.getLicense());
        param.put("openLicence",merchantEntity.getOpenLicence());
        param.put("institution",merchantEntity.getInstitution());
        param.put("value2",merchantEntity.getValue2());
        param.put("value5",merchantEntity.getValue5());
        param.put("value3",merchantEntity.getValue3());
        param.put("value4",merchantEntity.getValue4());
        param.put("remark",sysMerchant.getRemark());
        return R.ok().put("sysMerchant",param);
    }

    /**
     * 新增
     * @param param
     * @return
     */
    @RequestMapping("saveObjecet")
    public R saveObject(@RequestBody Map<String,Object> param){
        try {
            if(merchantInfoService.saveObject(param)==false){
                return R.error();
            }
            return R.ok();
        }catch (Exception e){
            e.printStackTrace();
            return R.error();
        }
    }

    /**
     * 查看信息
     */
    @RequestMapping("/info/{userId}")
    @RequiresPermissions("merchantinfo:info")
    public R info(@PathVariable("userId") Long userId) {
        MerchantInfoEntity sysMerchant = merchantInfoService.queryObject(userId);

        return R.ok().put("sysMerchant", sysMerchant);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("merchantinfo:save")
    public R save(@RequestBody MerchantInfoEntity sysMerchant) {
        sysMerchant.setPassword("92925488b28ab12584ac8fcaa8a27a0f497b2c62940c8f4fbc8ef19ebc87c43e");
        merchantInfoService.save(sysMerchant);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("merchantinfo:update")
    public R update(@RequestBody MerchantInfoEntity sysMerchant) {
        merchantInfoService.update(sysMerchant);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("merchantinfo:delete")
    public R delete(@RequestBody Long[] userIds) {
        Long userId = ShiroUtils.getUserId();
        for (Long userid : userIds){
            if(userid.equals(userId)){
                return R.ok().error("不可以删除自己");
            }
        }
        merchantInfoService.deleteBatch(userIds);
        return R.ok();
    }

    /**
     * 查看所有列表
     */
    @RequestMapping("/queryAll")
    public R queryAll(@RequestParam Map<String, Object> params) {

        List<MerchantInfoEntity> list = merchantInfoService.queryList(params);

        return R.ok().put("list", list);
    }
}
